package com.jzo2o.orders.manager.service.client;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jzo2o.api.foundations.ServeApi;
import com.jzo2o.api.foundations.dto.response.ServeAggregationResDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * TODO
 *
 * @author Zhy
 * @version 1.0
 * @date 2025-04-01 16:35
 */
@Component
@Slf4j
public class FoundationsClient {

	@Resource
	private ServeApi serveApi;

	/**
	 * 根据 ID 查询服务信息
	 *
	 * @return:
	 * @param:
	 */
	@SentinelResource(value = "serveDetail", fallback = "serveDetailFallback", blockHandler = "serveDetailBlockHandler")
	public ServeAggregationResDTO findById(Long serveId) {
		return serveApi.findById(serveId);
	}

	/**
	 * 降级方法
	 *
	 * @return:
	 * @param:
	 */
	public ServeAggregationResDTO serveDetailFallback(Long serveId, Throwable t) {
		log.error("根据 ID 查询服务信息失败，服务降级，返回默认值，错误信息：{}", t.getMessage());
		ServeAggregationResDTO dto = new ServeAggregationResDTO();
		dto.setId(-1L);
		return dto;
	}

	/**
	 * 熔断方法
	 *
	 * @return:
	 * @param:
	 */
	public ServeAggregationResDTO serveDetailBlockHandler(Long serveId, BlockException be) {
		log.error("根据 ID 查询服务信息失败，服务熔断，返回默认值，错误信息：{}", be.getMessage());
		ServeAggregationResDTO dto = new ServeAggregationResDTO();
		dto.setId(-2L);
		return dto;
	}

}
